package com.atguigu.distributed.lock.leecode;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * 力扣简单232.用栈实现队列
 * 请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作（push、pop、peek、empty）
 */
public class MyQueue {
    Deque<Integer> inStack;
    Deque<Integer> outStack;
    public MyQueue(){
        inStack = new ArrayDeque<>();
        outStack = new ArrayDeque<>();
    }
    public void push(int x){
        inStack.push(x);
    }
    public int pop(){
        if(outStack.isEmpty()){
            in2out();
        }
        return outStack.pop();
    }
    public int peek(){
        if(outStack.isEmpty()){
            in2out();
        }
        return outStack.peek();
    }
    public boolean empty(){
        return inStack.isEmpty()&&outStack.isEmpty();
    }
    private void in2out(){
        while(!inStack.isEmpty()){
            outStack.push(inStack.pop());
        }
    }
}
